package com.dykj.fireFighting.web.controller.GB26875Controller;

import com.dykj.fireFighting.web.enums.FlagDelEnum;
import com.dykj.fireFighting.web.model.Manufactor;
import com.dykj.fireFighting.web.service.ManufactorService;
import org.quickserver.net.AppException;
import org.quickserver.net.model.GBServerDataAll;
import org.quickserver.net.server.QuickServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@Component
public class StartController implements CommandLineRunner {
    @Autowired
    private ManufactorService manufactorService;
    @Override
    public void run(String... strings) throws Exception {
        new Thread() {
            public void run() {
                InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("properties/port.properties");
                Properties p = new Properties();
                try {
                    p.load(inputStream);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                //System.out.println("firePort:"+p.getProperty("firePort")+",waterPort:"+p.getProperty("waterPort"));
                int firePort=Integer.parseInt(p.getProperty("firePort"));
                //802主机
                QuickServer myServer =
                        new QuickServer("com.dykj.fireFighting.web.controller.GB26875Controller.GB26875Controller");
                myServer.setPort(firePort);
                myServer.setName("802 GB26875 v 1.0");
                myServer.setTimeout(300000);
                myServer.setClientData("org.quickserver.net.model.GBServerData");//连接存储数据
                myServer.setClientBinaryHandler("com.dykj.fireFighting.web.controller.GB26875Controller.GB26875Controller");
                Map<String, Object> map = new HashMap<>();
                map.put("flagDel", FlagDelEnum.notDel.getCode());
                List<Manufactor> list = manufactorService.queryList(map);
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getTemplate().equals("0101")) {
                        GBServerDataAll.devMap.put(list.get(i).getIdentification(), "0101"); //青鸟
                    } else if (list.get(i).getTemplate().equals("0103")) {
                        GBServerDataAll.devMap.put(list.get(i).getIdentification(), "0103"); //利达
                    } else if (list.get(i).getTemplate().equals("0102")) {
                        GBServerDataAll.devMap.put(list.get(i).getIdentification(), "0102"); //海湾
                    } else if (list.get(i).getTemplate().equals("0104")) {
                        GBServerDataAll.devMap.put(list.get(i).getIdentification(), "0104"); //水设备
                    }else if (list.get(i).getTemplate().equals("0105")) {
                        GBServerDataAll.devMap.put(list.get(i).getIdentification(), "0105"); //上海庄诚设备
                    }
                }

                //上海庄诚电设备
                int zcPort=Integer.parseInt(p.getProperty("zcPort"));
                QuickServer myServer1 =
                        new QuickServer("com.dykj.fireFighting.web.controller.GB26875Controller.GB26875Controller");
                myServer1.setPort(zcPort);
                myServer1.setName("上海庄诚 v 1.1");
                myServer1.setTimeout(300000);
                myServer1.setClientData("org.quickserver.net.model.ZCServerData");//连接存储数据
                myServer1.setClientBinaryHandler("com.dykj.fireFighting.web.controller.GB26875Controller.GB26875Controller");

                //苏州国网水设备
                int sygwPort=Integer.parseInt(p.getProperty("sygwPort"));
                QuickServer myServer2 = new QuickServer("com.dykj.fireFighting.web.controller.GB26875Controller.GB26875Controller");
                myServer2.setPort(sygwPort);
                myServer2.setName("苏州国网 v 1.0");
                myServer2.setTimeout(600000);
                myServer2.setClientBinaryHandler("com.dykj.fireFighting.web.controller.GB26875Controller.GB26875Controller");
                myServer2.setClientData("org.quickserver.net.model.GBServerDataSZGW");


                //盛善电气设备
                int ssPort=Integer.parseInt(p.getProperty("shengshanPort"));
                QuickServer myServer3 = new QuickServer("com.dykj.fireFighting.web.controller.GB26875Controller.GB26875Controller");
                myServer3.setPort(ssPort);
                myServer3.setName("盛善 v 1.0");
                myServer3.setTimeout(300000);
                myServer3.setClientBinaryHandler("com.dykj.fireFighting.web.controller.GB26875Controller.GB26875Controller");
                myServer3.setClientData("org.quickserver.net.model.ShengShanServerData");


                try {
                    myServer.startServer();
                    myServer1.startServer();
                    myServer2.startServer();
                    myServer3.startServer();
                } catch (
                        AppException e) {
                    System.err.println("Error in server : " + e);
                }
            }
        }.start();
    }
}
